package org.jeecg.modules.qyxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.hibernate.validator.constraints.NotBlank;
import org.jeecg.modules.qyxx.entity.BgFpHzNdJzdSxyyclcp;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.mapper.BgFpHzNdJzdSxyyclcpMapper;
import org.jeecg.modules.qyxx.service.IBgFpHzNdJzdSxyyclcpService;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 上下游原材料和产品集中度汇总数据
 * @Author: jeecg-boot
 * @Date:   2022-05-09
 * @Version: V1.0
 */
@Service
public class BgFpHzNdJzdSxyyclcpServiceImpl extends MppServiceImpl<BgFpHzNdJzdSxyyclcpMapper, BgFpHzNdJzdSxyyclcp> implements IBgFpHzNdJzdSxyyclcpService {
    @Autowired
    private IBgSjmkjgOrderService bgSjmkjgOrderService;

    @Override
    public List<BgFpHzNdJzdSxyyclcp> getTop5YclCpJzdZb(BgNsrjbxxReq req) {
        DateUtils utils = DateUtils.getInstance();
        Date jssj = req.getJssj();
       // String year = bgSjmkjgOrderService.getNewByDjxh(req.getDjxh());
        String year = utils.getYearToStr(jssj);
        List<String> tjnds = utils.nearlyThreeYears(req.getKssj(), req.getJssj());
        List<BgFpHzNdJzdSxyyclcp> list = baseMapper.getTop5YclCpJzdZb(req.getDjxh(), String.join(",", tjnds));
        /*LambdaQueryWrapper<BgFpHzNdJzdSxyyclcp> wrapper = Wrappers.lambdaQuery(BgFpHzNdJzdSxyyclcp.class);
        wrapper.eq(BgFpHzNdJzdSxyyclcp::getDjxh,req.getDjxh()).in(BgFpHzNdJzdSxyyclcp::getTjnd,tjnds);
        List<BgFpHzNdJzdSxyyclcp> list = super.list(wrapper);*/
        for (BgFpHzNdJzdSxyyclcp bgFpHzNdJzdSxyyclcp : list) {
                if (bgFpHzNdJzdSxyyclcp.getQbcpjyje() == null) {
                    bgFpHzNdJzdSxyyclcp.setQbcpjyje(BigDecimal.ZERO);
                } else {
                    bgFpHzNdJzdSxyyclcp.setQbcpjyje(bgFpHzNdJzdSxyyclcp.getQbcpjyje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }
                if (bgFpHzNdJzdSxyyclcp.getQbycljyje() == null) {
                    bgFpHzNdJzdSxyyclcp.setQbycljyje(BigDecimal.ZERO);
                } else {
                    bgFpHzNdJzdSxyyclcp.setQbycljyje(bgFpHzNdJzdSxyyclcp.getQbycljyje().divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
                }
                if (bgFpHzNdJzdSxyyclcp.getQwcpjyje() == null) {
                    bgFpHzNdJzdSxyyclcp.setQwcpjyje(BigDecimal.ZERO);
                } else {
                    bgFpHzNdJzdSxyyclcp.setQwcpjyje(bgFpHzNdJzdSxyyclcp.getQwcpjyje().divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
                }
                if (bgFpHzNdJzdSxyyclcp.getQwycljyje() == null) {
                    bgFpHzNdJzdSxyyclcp.setQwycljyje(BigDecimal.ZERO);
                } else {
                    bgFpHzNdJzdSxyyclcp.setQwycljyje(bgFpHzNdJzdSxyyclcp.getQwycljyje().divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
                }
            if (year.equals(bgFpHzNdJzdSxyyclcp.getTjnd())) {
                // String month = bgSjmkjgOrderService.getNewMonthByDjxh(req.getDjxh());
                String month = utils.getMonthToStr(jssj);
                String newMonth = month.substring(month.length() - 2);
                bgFpHzNdJzdSxyyclcp.setTjnd(year + "年(" + newMonth + "月)");
            } else {
                bgFpHzNdJzdSxyyclcp.setTjnd(bgFpHzNdJzdSxyyclcp.getTjnd() + "年");
            }
        }
        return list;
    }
}
